﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rankep.FilterableDataGridView;
using System.Data.Objects;

namespace DiveManager
{
    public partial class ManageActivitiesForm : Form
    {
        protected DiveDatabaseEntities diveDatabase;
        protected BindingSource bindingSource;

        // Item Filter
        private FilterItem textboxFilter;

        public ManageActivitiesForm()
        {
            InitializeComponent();

            // Init Database
            diveDatabase = new DiveDatabaseEntities();
            bindingSource = new BindingSource();
        }

        private void ManageActivitiesForm_Load(object sender, EventArgs e)
        {
            // Load lists
            LoadData();
            LoadDiveGuides();

            // Add Bindings
            txActivityName.DataBindings.Add(new Binding("Text", bindingSource, "ActivityName"));
            dtActivityDate.DataBindings.Add(new Binding("Value", bindingSource, "ActivityDate"));

            // Add Filtering
            textboxFilter = new FilterItem("", "ActivityName|ActivityDate");
        }

        protected void LoadData()
        {
            IQueryable<DiveActivities> dataQuery = (from a in diveDatabase.DiveActivities
                                                    orderby a.ActivityDate descending
                                                    select a);

            // Bind Data to BindingSource
            bindingSource.DataSource = dataQuery;

            // Bind BindingSource to List
            ActivityList.DataSource = bindingSource;

            // Hide ID-Columns
            ActivityList.Columns["ID"].Visible = false;
            ActivityList.Columns["RelationCustomersActivities"].Visible = false;
            ActivityList.Columns["RelationGuidesActivities"].Visible = false;

            // Set Column Headers
            ActivityList.Columns["ActivityName"].HeaderText = "Name";
            ActivityList.Columns["ActivityDate"].HeaderText = "Datum";
        }

        private void cmdFilter_TextChanged(object sender, EventArgs e)
        {
            //Change the filter Text
            textboxFilter.Filter = cmdFilter.Text;

            ActivityList.BeginFilterUpdate();
            try
            {
                //Add the filter
                ActivityList.Filters.Add(textboxFilter);
            }
            finally
            {
                ActivityList.EndFilterUpdate();
            }
        }

        private void LoadDiveGuides()
        {
            DiveActivities diveActivities = bindingSource.Current as DiveActivities;
            IQueryable<DiveGuides> diveGuideQuery = (from a in diveDatabase.DiveGuides
                                                     join b in diveDatabase.RelationGuidesActivities on a.ID equals b.DiveGuideID
                                                     where b.DiveActivityID == diveActivities.ID
                                                     select a);

            DiveGuideList.DataSource = diveGuideQuery;

            if (DiveGuideList.Columns.Count == 0)
                return;

            // Hide ID-Columns
            DiveGuideList.Columns["ID"].Visible = false;
            DiveGuideList.Columns["RelationGuidesActivities"].Visible = false;

            // Set Column Headers
            DiveGuideList.Columns["FirstName"].HeaderText = "Vorname";
            DiveGuideList.Columns["LastName"].HeaderText = "Nachname";
        }

        private void ManageActivitiesForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Save Data on FormClose
            bindingSource.EndEdit();
            diveDatabase.SaveChanges();
        }

        private void ActivityList_SelectionChanged(object sender, EventArgs e)
        {
            LoadDiveGuides();
        }

        private void cmdAddGuide_Click(object sender, EventArgs e)
        {
            // Find a Guide
            FindGuideForm findGuide = new FindGuideForm();

            if (findGuide.ShowDialog() == System.Windows.Forms.DialogResult.OK  && findGuide.SelectedGuide != Guid.Empty)
            {
                // Add Relation
                RelationGuidesActivities relationGuidesActivity = new RelationGuidesActivities();
                relationGuidesActivity.ID = Guid.NewGuid();
                relationGuidesActivity.DiveActivityID = ((DiveActivities)bindingSource.Current).ID;
                relationGuidesActivity.DiveGuideID = findGuide.SelectedGuide;
                diveDatabase.AddToRelationGuidesActivities(relationGuidesActivity);
                diveDatabase.SaveChanges();
                LoadDiveGuides();
            }
        }

        private void cmdDeleteGuide_Click(object sender, EventArgs e)
        {
            if (DiveGuideList.SelectedRows.Count < 1)
            {
                MessageBox.Show("Bitte einen Eintrag zum Löschen auswählen.");
                return;
            }

            if (MessageBox.Show("Den Guide wirklich löschen?", "Rückfrage", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                // Delete current Databaseentry
                Guid currentActivityID = ((DiveActivities)bindingSource.Current).ID;
                Guid currentGuideID = ((DiveGuides)DiveGuideList.SelectedRows[0].DataBoundItem).ID;
                RelationGuidesActivities guidActivities = (from a in diveDatabase.RelationGuidesActivities
                                                           where a.DiveActivityID == currentActivityID
                                                           && a.DiveGuideID == currentGuideID
                                                           select a).FirstOrDefault();

                diveDatabase.DeleteObject(guidActivities);
                diveDatabase.SaveChanges();
                LoadDiveGuides();
            }
        }

        private void cmdAddActivity_Click(object sender, EventArgs e)
        {
            // Add new DiveActivity
            DiveActivities diveActivity = new DiveActivities();
            diveActivity.ID = Guid.NewGuid();
            diveActivity.ActivityName = "<neue Aktivität>";
            diveActivity.ActivityDate = new DateTime(2099, 1, 1);

            diveDatabase.AddToDiveActivities(diveActivity);
            diveDatabase.SaveChanges();
            LoadData();
        }

        private void cmdDeleteActivity_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Die Aktivität wirklich löschen?", "Rückfrage", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                if (bindingSource.Current != null)
                {
                    DiveActivities diveActivity = bindingSource.Current as DiveActivities;
                    diveDatabase.DeleteObject(diveActivity);
                    diveDatabase.SaveChanges();
                    LoadData();
                }
            }
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
    }
}
